package cn.itcast.crm.service.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.itcast.crm.dao.CustomerRepository;
import cn.itcast.crm.domain.Customer;
import cn.itcast.crm.service.CustomerService;
@Service
@Transactional
public class CustomerServiceImpl implements CustomerService {
	//依赖注入
    @Autowired
    private CustomerRepository customerRepository;
	@Override
	public List<Customer> findNoAssociationCustomer() {
		// TODO Auto-generated method stub
		
		return customerRepository.findByFixedAreaIdIsNull();
	}

	@Override
	public List<Customer> findHasAssociationFixedAreaCustomers(String fixedAreaId) {
		// TODO Auto-generated method stub
		return customerRepository.findByFixedAreaId(fixedAreaId);
	}

	@Override//1,2,3,4
	public void associationCustomersToFixedArea(String customerIdStr, String fixedAreaId) {
		// TODO Auto-generated method stub
		//解除关联动作
		System.out.println(11111);
		customerRepository.clearFixedAreaId(fixedAreaId);
		System.out.println(11111);
		//切割客户id
		if(StringUtils.isBlank(customerIdStr)||customerIdStr.equals("null")){
			return;
		}
		System.out.println(customerIdStr);
		String[] customerId = customerIdStr.split(",");
		for (String cid : customerId) {
			System.out.println(cid);
			Integer id = Integer.parseInt(cid);
		//根据客户修改定区id
			customerRepository.updateFixedAreaId(fixedAreaId, id);
		}

	}

}
